{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 常用快捷键"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 处于命令模式时"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Enter : 转入编辑模式\n",
    "\n",
    "- *Shift-Enter : 运行本单元，选中下个单元*\n",
    "- *Ctrl-Enter : 运行本单元*\n",
    "- *Alt-Enter : 运行本单元，在其下插入新单元*\n",
    "\n",
    "- Y : 单元转入代码状态\n",
    "\n",
    "- M :单元转入markdown状态\n",
    "\n",
    "- *A : 在上方插入新单元*\n",
    "- *B : 在下方插入新单元*\n",
    "\n",
    "- X : 剪切选中的单元\n",
    "- C : 复制选中的单元\n",
    "- Shift-Space : 向上滚动\n",
    "- Space : 向下滚动"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 处于编辑模式时"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Tab : 代码补全或缩进\n",
    "- *Shift-Tab : 提示*\n",
    "- *Ctrl-] : 缩进*\n",
    "- Ctrl-[ : 解除缩进\n",
    "- Ctrl-Up : 跳到单元开头\n",
    "- Ctrl-End : 跳到单元末尾\n",
    "- Ctrl-Down : 跳到单元末尾\n",
    "- Ctrl-Left : 跳到左边一个字首\n",
    "- Ctrl-Right : 跳到右边一个字首\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 代码补全操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 例子1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "name_age = [\"zhichao_18\",\"Alex_23\",\"Marry_25\",\"Hen-teng_28\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'zhichao': '18', 'Alex': '23', 'Marry': '25', 'Hen-teng': '28'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1⃣️ 使用tab补全，或者下方提示（基本与pycharm相同）\n",
    "# ↓以'_'切片，取值\n",
    "\n",
    "dict_person = {}\n",
    "#方法1:\n",
    "# for循环添加\n",
    "# for i in name_age:\n",
    "#     dict_person[n.split('_')[0]] = n.split('_')[1]\n",
    "# dict_person\n",
    "\n",
    "#方法2:\n",
    "#字典推导式\n",
    "dict_person = {i.split('_')[0]:i.split('_')[1] for i in name_age}\n",
    "dict_person"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 例子2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数补全\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-03-20\n"
     ]
    }
   ],
   "source": [
    "print(datetime.date(2020,3,20))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "395 days, 0:00:00\n"
     ]
    }
   ],
   "source": [
    "# 计算相差日期\n",
    "date1 = datetime.date(2020,2,1)\n",
    "data2 = datetime.date(2019,1,2)\n",
    "print(date1.__sub__(data2)) \n",
    "# __sub__() 内置函数，两个对象相减，对应为__add__函数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-02-01\n"
     ]
    }
   ],
   "source": [
    "# 格式化日期\n",
    "print(date1.strftime(\"%Y-%m-%d\")) \n",
    "# strftime 为 格式化日期函数，详见:https://www.cnblogs.com/xuzhichao/p/11407192.html"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 例子3 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关键字参数补全\n",
    "def func_with_keywords(name=None,age=18):\n",
    "    return name,age"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('aaa', 18)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "func_with_keywords(name=\"aaa\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# %run命令"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 可以通过    %run 文件名  来运行py文件\n",
    "%run ipython_test.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 魔术命令"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "详见[博客讲解](https://www.cnblogs.com/bind/p/11958317.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 示例1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 5.23 ms, sys: 7.16 ms, total: 12.4 ms\n",
      "Wall time: 33.2 ms\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[  0.2488361 ,  11.49854925,   9.41411829, ...,   9.82632058,\n",
       "          2.79722458,  -2.68301743],\n",
       "       [  2.56527297,  17.91324588,  -0.85716434, ..., -35.4722598 ,\n",
       "         -6.5444375 ,   3.80176737],\n",
       "       [  4.74663039,  -5.73201859,   4.6421433 , ...,   5.78972385,\n",
       "        -12.82085553,   6.03515182],\n",
       "       ...,\n",
       "       [ -2.87328523,   2.45948238,  14.33904916, ...,  13.18280986,\n",
       "        -19.61478922,  -2.53909576],\n",
       "       [-24.40687412, -18.35502672,  -0.91213827, ...,   4.47325558,\n",
       "          7.21197627,  -7.94489436],\n",
       "       [-14.37555246, -23.22978824,  -0.1080302 , ..., -12.73875566,\n",
       "        -11.82350325,  -3.6823544 ]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "# 100随机矩阵 点积运算 \n",
    "a = np.random.randn(100,100)\n",
    "%time np.dot(a,a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 爬取*博客魔术命令*演示表格"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 魔术命令"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1;33m魔术命令-Line magics\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>命令</th>\n",
       "      <th>详情</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>%alias</td>\n",
       "      <td>定义别名</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>%alias_magic</td>\n",
       "      <td>为现有的魔术命令创建别名</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>%autocall</td>\n",
       "      <td>%autocall</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>%automagic</td>\n",
       "      <td>设置输入魔术命令时是否键入%前缀，on(1)/off(0)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>%bookmark</td>\n",
       "      <td>管理IPython的书签系统</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>%cd</td>\n",
       "      <td>更改当前工作目录</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>%colors</td>\n",
       "      <td>%colors</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>%config</td>\n",
       "      <td>%config</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>%debug</td>\n",
       "      <td>%debug</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>%dhist</td>\n",
       "      <td>打印历史访问目录</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>%dirs</td>\n",
       "      <td>返回当前目录堆栈</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>%doctest_mode</td>\n",
       "      <td>%doctest_mode</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>%edit</td>\n",
       "      <td>%edit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>%env</td>\n",
       "      <td>设置环境变量(无需重启)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>%gui</td>\n",
       "      <td>%gui</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>%history</td>\n",
       "      <td>%history</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>%killbgscripts</td>\n",
       "      <td>%killbgscripts</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>%load</td>\n",
       "      <td>导入python文件</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>%load_ext</td>\n",
       "      <td>%load_ext</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>%loadpy</td>\n",
       "      <td>%load别名</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>%logoff</td>\n",
       "      <td>临时停止logging</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>%logon</td>\n",
       "      <td>重新开始logging</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>%logstart</td>\n",
       "      <td>%logstart</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>%logstate</td>\n",
       "      <td>%logstate</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>%lsmagic</td>\n",
       "      <td>列出当前可用的魔术命令。</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>%macro</td>\n",
       "      <td>定义用来重复执行的宏</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>%magic</td>\n",
       "      <td>显示魔术命令的帮助</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>%matplotlib</td>\n",
       "      <td>设置matplotlib的工作方式</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>%notebook</td>\n",
       "      <td>%notebook</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>%page</td>\n",
       "      <td>%page</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>%pastebin</td>\n",
       "      <td>%pastebin</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>%pdb</td>\n",
       "      <td>控制pdb交互式调试器的自动调用</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                命令                             详情\n",
       "0           %alias                           定义别名\n",
       "1     %alias_magic                   为现有的魔术命令创建别名\n",
       "2        %autocall                      %autocall\n",
       "3       %automagic  设置输入魔术命令时是否键入%前缀，on(1)/off(0)\n",
       "4        %bookmark                 管理IPython的书签系统\n",
       "5              %cd                       更改当前工作目录\n",
       "6          %colors                        %colors\n",
       "7          %config                        %config\n",
       "8           %debug                         %debug\n",
       "9           %dhist                       打印历史访问目录\n",
       "10           %dirs                       返回当前目录堆栈\n",
       "11   %doctest_mode                  %doctest_mode\n",
       "12           %edit                          %edit\n",
       "13            %env                   设置环境变量(无需重启)\n",
       "14            %gui                           %gui\n",
       "15        %history                       %history\n",
       "16  %killbgscripts                 %killbgscripts\n",
       "17           %load                     导入python文件\n",
       "18       %load_ext                      %load_ext\n",
       "19         %loadpy                        %load别名\n",
       "20         %logoff                    临时停止logging\n",
       "21          %logon                    重新开始logging\n",
       "22       %logstart                      %logstart\n",
       "23       %logstate                      %logstate\n",
       "24        %lsmagic                   列出当前可用的魔术命令。\n",
       "25          %macro                     定义用来重复执行的宏\n",
       "26          %magic                      显示魔术命令的帮助\n",
       "27     %matplotlib              设置matplotlib的工作方式\n",
       "28       %notebook                      %notebook\n",
       "29           %page                          %page\n",
       "30       %pastebin                      %pastebin\n",
       "31            %pdb               控制pdb交互式调试器的自动调用"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "from lxml import etree\n",
    "import pandas as pd\n",
    "\n",
    "# requests 爬取网页\n",
    "url = (\"https://www.cnblogs.com/bind/p/11958317.html\")\n",
    "response = requests.get(url)\n",
    "\n",
    "# etree  html语法纠正\n",
    "response_elements = etree.HTML(response.text)\n",
    "\n",
    "## //*[@id=\"cnblogs_post_body\"]/table[1]\n",
    "# //*[@id=\"cnblogs_post_body\"]/table[1]\n",
    "\n",
    "# xpath定位\n",
    "table_magic = response_elements.xpath(\"//*[@id=\\\"cnblogs_post_body\\\"]/table[1]\") \n",
    "table = etree.tostring(table_magic[0], encoding='utf-8').decode()\n",
    "# Web数据挖掘相关\n",
    "\n",
    "\n",
    "# 数据分析简单的使用\n",
    "# pd 表格输出csv'\n",
    "df = pd.read_html(table, encoding='utf-8', header=0)[0]\n",
    "results = list(df.T.to_dict().values())\n",
    "df.to_csv(\"magic_table.csv\", index=False)\n",
    "\n",
    "\n",
    "# pd 读取csv\n",
    "magic_data = pd.read_csv(\"magic_table.csv\")\n",
    "\n",
    "#python 颜色： \\033[显示方式;前景色;背景色m + 结尾部分：\\033[0m \n",
    "#显示方式: 0（默认\\）、1（高亮）、22（非粗体）、4（下划线）、24（非下划线）、 5（闪烁）、25（非闪烁）、7（反显）、27（非反显）\n",
    "#前景色:   30（黑色）、31（红色）、32（绿色）、 33（黄色）、34（蓝色）、35（洋 红）、36（青色）、37（白色）\n",
    "#背景色:   40（黑色）、41（红色）、42（绿色）、 43（黄色）、44（蓝色）、45（洋 红）、46（青色）、47（白色）\n",
    "print(\"\\033[1;33m魔术命令-Line magics\\033[0m\")\n",
    "magic_data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 打印命令"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1;31m魔术命令-打印相关\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>命令</th>\n",
       "      <th>详情</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>%pdef</td>\n",
       "      <td>打印任何可调用对象信息</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>%pdoc</td>\n",
       "      <td>打印对象的docstring</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>%pfile</td>\n",
       "      <td>%pfile</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>%pinfo</td>\n",
       "      <td>%pinfo</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>%pinfo2</td>\n",
       "      <td>%pinfo2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>%pip</td>\n",
       "      <td>运行pip命令</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>%popd</td>\n",
       "      <td>%popd</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>%pprint</td>\n",
       "      <td>美化打印</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>%precision</td>\n",
       "      <td>设置美化打印时的浮点数精度</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>%profile</td>\n",
       "      <td>打印您当前活动的IPython配置文件</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>%prun</td>\n",
       "      <td>告诉你程序中每个函数消耗的时间</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>%psearch</td>\n",
       "      <td>%psearch</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>%psource</td>\n",
       "      <td>打印对象源代码</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>%pushd</td>\n",
       "      <td>%pushd</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>%pwd</td>\n",
       "      <td>返回当前工作路径</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>%pycat</td>\n",
       "      <td>%pycat</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>%pylab</td>\n",
       "      <td>加载numpy、matplotlib</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>%quickref</td>\n",
       "      <td>%quickref</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>%recall</td>\n",
       "      <td>%recall</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>%rehashx</td>\n",
       "      <td>%rehashx</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            命令                   详情\n",
       "0        %pdef          打印任何可调用对象信息\n",
       "1        %pdoc       打印对象的docstring\n",
       "2       %pfile               %pfile\n",
       "3       %pinfo               %pinfo\n",
       "4      %pinfo2              %pinfo2\n",
       "5         %pip              运行pip命令\n",
       "6        %popd                %popd\n",
       "7      %pprint                 美化打印\n",
       "8   %precision        设置美化打印时的浮点数精度\n",
       "9     %profile  打印您当前活动的IPython配置文件\n",
       "10       %prun      告诉你程序中每个函数消耗的时间\n",
       "11    %psearch             %psearch\n",
       "12    %psource              打印对象源代码\n",
       "13      %pushd               %pushd\n",
       "14        %pwd             返回当前工作路径\n",
       "15      %pycat               %pycat\n",
       "16      %pylab   加载numpy、matplotlib\n",
       "17   %quickref            %quickref\n",
       "18     %recall              %recall\n",
       "19    %rehashx             %rehashx"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "table_magic_2 = response_elements.xpath(\"//*[@id=\\\"cnblogs_post_body\\\"]/table[2]\") \n",
    "table_2 = etree.tostring(table_magic_2[0], encoding='utf-8').decode()\n",
    "\n",
    "# pd 表格输出csv'\n",
    "df_2 = pd.read_html(table_2, encoding='utf-8', header=0)[0]\n",
    "results = list(df_2.T.to_dict().values())\n",
    "df_2.to_csv(\"magic_table_2.csv\", index=False)\n",
    "# pd 读取csv\n",
    "magic_data_2 = pd.read_csv(\"magic_table_2.csv\")\n",
    "\n",
    "# python 颜色： \\033[显示方式;前景色;背景色m + 结尾部分：\\033[0m \n",
    "#显示方式: 0（默认\\）、1（高亮）、22（非粗体）、4（下划线）、24（非下划线）、 5（闪烁）、25（非闪烁）、7（反显）、27（非反显）\n",
    "#前景色:   30（黑色）、31（红色）、32（绿色）、 33（黄色）、34（蓝色）、35（洋 红）、36（青色）、37（白色）\n",
    "#背景色:   40（黑色）、41（红色）、42（绿色）、 43（黄色）、44（蓝色）、45（洋 红）、46（青色）、47（白色）\n",
    "print(\"\\033[1;31m魔术命令-打印相关\\033[0m\")\n",
    "magic_data_2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 运行命令"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1;31m魔术命令-运行相关\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>命令</th>\n",
       "      <th>详情</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>%reload_ext</td>\n",
       "      <td>通过其模块名称重新加载IPython扩展</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>%rerun</td>\n",
       "      <td>%rerun</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>%reset</td>\n",
       "      <td>%reset</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>%reset_selective</td>\n",
       "      <td>%reset_selective</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>%run</td>\n",
       "      <td>%run</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>%save</td>\n",
       "      <td>%save</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>%sc</td>\n",
       "      <td>%sc</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>%set_env</td>\n",
       "      <td>设置环境变量</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>%sx</td>\n",
       "      <td>%sx</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>%system</td>\n",
       "      <td>%system</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>%tb</td>\n",
       "      <td>%tb</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>%time</td>\n",
       "      <td>执行Python语句或表达式的时间</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>%timeit</td>\n",
       "      <td>%timeit</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>%unalias</td>\n",
       "      <td>移别名</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>%unload_ext</td>\n",
       "      <td>通过其模块名称卸载IPython扩展</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>%who</td>\n",
       "      <td>列出全局变量</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>%who_ls</td>\n",
       "      <td>以排序列表的方式列出变量</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>%whos</td>\n",
       "      <td>类似who，但给出的信息更详细</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>%xdel</td>\n",
       "      <td>%xdel</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>%xmode</td>\n",
       "      <td>%xmode</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  命令                    详情\n",
       "0        %reload_ext  通过其模块名称重新加载IPython扩展\n",
       "1             %rerun                %rerun\n",
       "2             %reset                %reset\n",
       "3   %reset_selective      %reset_selective\n",
       "4               %run                  %run\n",
       "5              %save                 %save\n",
       "6                %sc                   %sc\n",
       "7           %set_env                设置环境变量\n",
       "8                %sx                   %sx\n",
       "9            %system               %system\n",
       "10               %tb                   %tb\n",
       "11             %time     执行Python语句或表达式的时间\n",
       "12           %timeit               %timeit\n",
       "13          %unalias                   移别名\n",
       "14       %unload_ext    通过其模块名称卸载IPython扩展\n",
       "15              %who                列出全局变量\n",
       "16           %who_ls          以排序列表的方式列出变量\n",
       "17             %whos       类似who，但给出的信息更详细\n",
       "18             %xdel                 %xdel\n",
       "19            %xmode                %xmode"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "table_magic_3 = response_elements.xpath(\"//*[@id=\\\"cnblogs_post_body\\\"]/table[3]\") \n",
    "table_3 = etree.tostring(table_magic_3[0], encoding='utf-8').decode()\n",
    "\n",
    "# pd 表格输出csv'\n",
    "df_3 = pd.read_html(table_3, encoding='utf-8', header=0)[0]\n",
    "results = list(df_2.T.to_dict().values())\n",
    "df_3.to_csv(\"magic_table_3.csv\", index=False)\n",
    "# pd 读取csv\n",
    "magic_data_3 = pd.read_csv(\"magic_table_3.csv\")\n",
    "\n",
    "# python 颜色： \\033[显示方式;前景色;背景色m + 结尾部分：\\033[0m \n",
    "#显示方式: 0（默认\\）、1（高亮）、22（非粗体）、4（下划线）、24（非下划线）、 5（闪烁）、25（非闪烁）、7（反显）、27（非反显）\n",
    "#前景色:   30（黑色）、31（红色）、32（绿色）、 33（黄色）、34（蓝色）、35（洋 红）、36（青色）、37（白色）\n",
    "#背景色:   40（黑色）、41（红色）、42（绿色）、 43（黄色）、44（蓝色）、45（洋 红）、46（青色）、47（白色）\n",
    "print(\"\\033[1;31m魔术命令-运行相关\\033[0m\")\n",
    "magic_data_3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Matplotlib"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Matplotlib 是一个 Python 的 2D绘图库，它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
